package com.project.base.baseproject.AppCustoms.ActionSheet;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.project.base.baseproject.AppCustoms.MyApplication.MyApplication;
import com.project.base.baseproject.R;

import org.w3c.dom.Text;

import java.util.ArrayList;


import static android.view.View.inflate;

/**
 * Created by mac on 2017/5/2.
 */
//模仿iOS的ActionSheet
public class ActionSheet {
    private Dialog mLoadingDialog;
    ActionSheetClickListener actionSheetClickListener;
    public ActionSheet(final Context context, String title, final ArrayList<String> arrayList){
        // 首先得到整个View
        final View view = LayoutInflater.from(context).inflate(
                R.layout.action_sheet_layout, null);
        // 创建自定义样式的Dialog
        this.mLoadingDialog = new Dialog(context,R.style.MyDialog);
        this.mLoadingDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.mLoadingDialog.setContentView(view);
        //设置内容
        TextView titleView = (TextView) view.findViewById(R.id.topTextView);
        titleView.setText(title);
        ListView listView = (ListView) view.findViewById(R.id.commonListview);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if(actionSheetClickListener != null)
                    actionSheetClickListener.onItemClick(view,i);
            }
        });
        listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return arrayList.size();
            }
            @Override
            public Object getItem(int i) {
                return i;
            }
            @Override
            public long getItemId(int i) {
                return i;
            }
            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                //得到我的圈子表格视图的cell对象
                View currView = inflate(context, R.layout.more_select_view_item, null);
                TextView textView = (TextView) currView.findViewById(R.id.moreSelectViewItemTextView);
                textView.setText(arrayList.get(i));
                return currView;
            }
        });
        //设置点击事件
        view.findViewById(R.id.cancleBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                close();
            }
        });
        //设置背景色
        View view1 = view.findViewById(R.id.cancle_textview_bg);
        GradientDrawable upTextGrad = (GradientDrawable)view1.getBackground();
        upTextGrad.setColor(MyApplication.getInstance().getResources().getColor(R.color.white));

        LinearLayout linearLayout = (LinearLayout)view.findViewById(R.id.listview_bg);
        GradientDrawable upTextGrad1 = (GradientDrawable)linearLayout.getBackground();
        upTextGrad1.setColor(MyApplication.getInstance().getResources().getColor(R.color.white));
    }
    public void show(){
        this.mLoadingDialog.show();
    }

    public void close(){
        if (this.mLoadingDialog!=null) {
            this.mLoadingDialog.dismiss();
        }
    }
    //设置响应者
    public void setActionSheetClickListener(ActionSheetClickListener listener) {
        this.actionSheetClickListener = listener;
    }
    //define interface
    public static interface ActionSheetClickListener {
        void onItemClick(View view , int index);
    }
}
